package com.iris.live.services.data.repositories.impl;

import com.iris.live.services.common.JDBCILogUtils;
import com.iris.live.services.data.repositories.ILogInfoRepository;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/5/31.
 */
@Repository
public class ILogInfoRepositoryImpl extends BaseRepository implements ILogInfoRepository {

    @Override
    public List getLiveScInfo(String permit, List<String> list) {
        StringBuffer sb = new StringBuffer();
        Map param = new HashMap();
        param.put("permit", permit);
        sb.append(" SELECT " +
                " 	a.`人员姓名` userName, " +
                " 	a.`用户名` userEmail " +
                " from `用户` a " +
                " WHERE a.许可 = :permit " +
                " AND a.`角色` = '销售顾问' ");
        if (list != null && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                if (i == 0) {
                    sb.append(" AND a.`人员姓名` in (");
                }
                sb.append("'" + list.get(i) + "'");
                if (i != list.size() - 1) {
                    sb.append(",");
                }
            }
        }
        sb.append(") ");
        return this.getList(sb.toString(), param);

    }

    @Override
    public boolean updateEmailInfoAboutSales(String permit, String userEmail, String id, String tableName) {
        String sb = "UPDATE " + tableName + " a SET a.`电子邮箱` = ? WHERE a.`用户名` = ?";
        String[] param = new String[2];
        param[0] = userEmail;
        param[1] = id;
        return JDBCILogUtils.runUpdateSql(sb, param);
    }

    @Override
    public boolean updateScInfoAboutSales(String tableName) {
        String sb = "UPDATE " + tableName + " a SET a.`用户名` = a.`电子邮箱`";
        return JDBCILogUtils.runUpdateSql(sb);
    }

    @Override
    public boolean updateScInfoAboutReceptionRecord(String permit, String tableNameUpdate,String tableNameSource) {
        String sb = "UPDATE " + tableNameUpdate + " a, " + tableNameSource + " b SET a.`销售顾问` = b.`电子邮箱` WHERE a.`经销店` = ? AND a.`销售顾问` = b.`用户名`";
        String[] param = new String[1];
        param[0] = permit;
        return JDBCILogUtils.runUpdateSql(sb, param);
    }

    @Override
    public boolean updateAppointScInfoAboutReceptionRecord(String permit, String tableNameUpdate,String tableNameSource) {
        String sb = "UPDATE " + tableNameUpdate + " a, " + tableNameSource + " b SET a.`是否指定销售顾问` = b.`电子邮箱` WHERE a.`经销店` = ? AND a.`是否指定销售顾问` = b.`用户名`";
        String[] param = new String[1];
        param[0] = permit;
        return JDBCILogUtils.runUpdateSql(sb, param);
    }

    @Override
    public boolean updateScInfoAboutWorkStatistics(String permit, String tableNameUpdate,String tableNameSource) {
        String sb = "UPDATE " + tableNameUpdate + " a, " + tableNameSource + " b SET a.`销售顾问` = b.`电子邮箱` WHERE a.`经销店` = ? AND a.`销售顾问` = b.`用户名`";
        String[] param = new String[1];
        param[0] = permit;
        return JDBCILogUtils.runUpdateSql(sb, param);
    }

    @Override
    public boolean updateScInfoAboutSaleTarget(String permit, String tableNameUpdate,String tableNameSource) {
        String sb = "UPDATE " + tableNameUpdate + " a, " + tableNameSource + " b SET a.`销售顾问` = b.`电子邮箱` WHERE a.`经销商代码` = ? AND a.`销售顾问` = b.`用户名`";
        String[] param = new String[1];
        param[0] = permit;
        return JDBCILogUtils.runUpdateSql(sb, param);
    }

    @Override
    public List getILogtableName() {
        String sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'ilog'";
        return JDBCILogUtils.runSelectSql(sql);
    }

    @Override
    public List getScInfoByScName(String permit, String scName) {
        String sql = "SELECT a.`用户名` from `用户` a where a.`许可` = :permit AND a.`人员姓名` = :scName";
        Map param = new HashMap();
        param.put("permit", permit);
        param.put("scName", scName);
        return this.getList(sql, param);
    }
}
